package com.sirius.test.rabbit;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * 有选择的接受消息
 */
public class TopicSend {

    private static final String EXCHANGE_NAME = "topic_logs";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();    // 连接工厂
        factory.setHost("192.168.231.128");
		factory.setUsername("admin");
		factory.setPassword("admin");
		factory.setPort(5672);
        Connection connection = factory.newConnection();        // 获取连接
        Channel channel = connection.createChannel();

        // topic类型
        channel.exchangeDeclare(EXCHANGE_NAME, "topic");

        String[] msg = {"666的兔子","懒惰的大象"};
        // 第二个参数为绑定键
        channel.basicPublish(EXCHANGE_NAME, "quick.orange.rabbit", null, msg[0].getBytes());
        channel.basicPublish(EXCHANGE_NAME, "lazy.write.elephant", null, msg[1].getBytes());
        System.out.println("PS-Send:" + msg.toString());

        channel.close();
        connection.close();

    }
}
